class Solution {
public:
    vector<int> father,size;
    int find(int u){
        return u == father[u] ? u : find(father[u]);
    }
    void Union(int u,int v){
        int x = find(u);
        int y = find(v);
        if(x == y) return;
        
        father[y] = x;
        size[x] += size[y];
    }
    
    int maxAreaOfIsland(vector<vector<int>>& grid) {
        int m = grid.size();
        int n = grid[0].size();
        father.resize(m*n);
        size.resize(m*n,1);
        for(int i = 0;i < m*n;i++){
            father[i] = i;
        }
        
        for(int i = 0;i < m;i++){
            for(int j = 0;j < n;j++){
                if(grid[i][j] == 0){
                    father[i*n+j] = 0;
                    size[i*n+j] = 0;
                    continue;
                }
                if(i > 0 && grid[i-1][j]){
                    Union((i-1)*n+j,i*n+j);
                }
                if(j > 0 && grid[i][j-1]){
                    Union(i*n+j-1,i*n+j);
                }
            }
        }
        
        int ret = 0;
        for(auto& v : size){
            ret = max(ret,v);
        }
        return ret;
    }
};
